#include "viewswitchbutton.h"
#include "ui_viewswitchbutton.h"
#include <QDebug>

ViewSwitchButton::ViewSwitchButton(QWidget *parent, int index) :
    QWidget(parent),
    _index(index),
    _selected(false),
    ui(new Ui::ViewSwitchButton)
{
    ui->setupUi(this);
    setText();
    selected(false);
    qDebug() << "new a button, index = " << _index;
}

ViewSwitchButton::~ViewSwitchButton()
{
    delete ui;
}

void ViewSwitchButton::selected(bool selected)
{
    _selected = selected;
    setIcon();
    setStyle();
}

void ViewSwitchButton::setIcon(const QString &_path)
{
    QString path(_path);
    if(path.isEmpty()){
        path = ":/image/ui/viewswitch/";
//        switch (_index) { case 0: path += "recommend"; break; case 1: path += "featured"; break; case 2: path += "podcast"; break; case 3: path += "explore"; break; case 4: path += "follow"; break; default: path += "icon"; }
        path += "icon";
        if(_selected) path += "_r.png";
        else path += "_w.png";
    }
    ui->icon->setPixmap(QPixmap(path));
}

void ViewSwitchButton::setStyle()
{
    if(_selected == false) {
        ui->text->setStyleSheet("color: #787F8D;");
        ui->ViewSwitchButtonStyle->setStyleSheet ("#ViewSwitchButtonStyle{ background-color : #F0F3F6; border : none; border-radius: 12px; }#ViewSwitchButtonStyle:hover { background-color:#E4E8EC; }");
    } else {
        ui->text->setStyleSheet("color: #ffffff;");
        ui->ViewSwitchButtonStyle->setStyleSheet ("#ViewSwitchButtonStyle{ background-color : #Fc3c4E; border : none; border-radius: 12px; }");
    }
}

void ViewSwitchButton::setText(const QString &_text)
{
    QString text(_text);
    if(text.isEmpty()){
        switch (_index){
        case 0: text = "推荐"; break;
        case 1: text = "精选"; break;
        case 2: text = "播客"; break;
        case 3: text = "漫游"; break;
        case 4: text = "关注"; break;
        case 5: text = "我喜欢的音乐"; break;
        case 8: text = "本地音乐"; break;
        default: text = "未设置文本";
        }
    }

    QFont font = ui->text->font(); // 设置绝对间距为0px
    font.setLetterSpacing(QFont::AbsoluteSpacing, 0);
    ui->text->setFont(font);

    ui->text->setText(text);
}

void ViewSwitchButton::on_pushButton_clicked()
{
    qDebug() << "clicked";
    emit viewSwitchButton_clicked(_index);
}
